//
//  SRDataViewController.m
//  SportRing
//
//  Created by elwin on 10/30/14.
//  Copyright (c) 2014 HotOne. All rights reserved.
//

#import "SRDataViewController.h"
#import "SRSportShareViewController.h"
#import "SRDataView.h"
#import "SRSelectDateView.h"
#import "SRSportDetailViewController.h"
#import "SRMonth+Cate.h"
#import "SRMe+Cate.h"
#import "SRSportPart+Cate.h"
#import "SRDay+Cate.h"
#import "NSDate+Additionals.h"
#import "SRSportNetService.h"
#import "NSDate+Additionals.h"
#import "SRSportTask.h"

@interface SRDataViewController ()<SRSelectDateViewDelegate,SRDataViewDelegate>

@property (nonatomic, weak) IBOutlet SRDataView *srDataView;

@property (nonatomic, weak) IBOutlet SRSelectDateView * srSelectDateView;

@property (nonatomic, strong)SRMonth * month;

@property (nonatomic, strong)SRDataViewModel *dataViewModel;


@end

@implementation SRDataViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //添加navigationbar右边分享按钮
    [self addRightImageButton:@"share_icon"];
    
    //设置选择日期的显示方式：显示月份
    _srSelectDateView.dateType = SELECTE_DATE_TYPE_MONTH;
    
    [self getDataFromServerByMonth:_srSelectDateView.selectedDate];
    
    _dataViewModel = [[SRDataViewModel alloc] init];

}

- (void)getDataFromServerByMonth:(NSDate *)date
{
    NSString *monthStr = [NSDate stringFromDate:date format:@"yyyy-MM-dd"];
    
    [SVProgressHUD show];
    
    [[[SRSportNetService getDaySportForMonth:monthStr] flattenMap:^RACStream *(id value) {
        return [SRSportNetService getTaskStatisticsByMonth:monthStr];
    }] subscribeNext:^(id x) {
        
        [SVProgressHUD dismiss];
        
        [self setupDataForMonth:_srSelectDateView.selectedDate];
        
    } error:^(NSError *error) {
        
        [SVProgressHUD showErrorWithStatus:@"数据拉取失败"];
    }];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)setupDataForMonth:(NSDate *)date
{
    _month = [SRMonth findMonthCINEForDate:date forMe:[SRMe currentUser]];
    
    _dataViewModel.distance = [_month countOfDistance] / 5000000;
    _dataViewModel.steps = [_month countOfSteps];
    _dataViewModel.kalorie = [_month countOfCalorie];
    _dataViewModel.creatDate = [NSDate dateFromString:_month.title format:@"yyyy-MM"];
    _dataViewModel.process = [_month processOfTasks];
    
    NSMutableArray *monthDatas = [[NSMutableArray alloc] init];
    
    NSArray *daysForMonth = [SRDay findDaysForMonth:_month];
    
    [daysForMonth enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        
        SRDay *day = (SRDay *)obj;
        
        SRDataViewModel *dataViewModel = [[SRDataViewModel alloc] init];
        dataViewModel.distance = [day countOfDistance];
        dataViewModel.steps = [day countOfSteps];
        dataViewModel.creatDate = [NSDate dateFromString:day.createDate format:nil];
        
        [monthDatas addObject:dataViewModel];
    }];
    
    SRDataViewModels *models = [[SRDataViewModels alloc] init];
    models.dataArray = monthDatas;
    
    [_srDataView setSRDataViewData:models total:_dataViewModel];
}

-(void)onRightButtonPressed
{
    UIStoryboard *SportRingMainBoard = [UIStoryboard storyboardWithName:[SRSourceConfig uiMainStoryBoard] bundle:nil];
    
    SRSportShareViewController *next = [SportRingMainBoard instantiateViewControllerWithIdentifier:[SRSourceConfig uiShareInterface]];
    
    next.date = _srSelectDateView.selectedDate;

    next.shareType = SR_SHARE_TYPE_MONTH_SPORT;
    
    SRSportShareContentForMonthViewModel *model = [[SRSportShareContentForMonthViewModel alloc] init];
    
    model.distance = [_month countOfDistance] / 5000000;
    model.steps = [_month countOfSteps];
    model.kalorie = [_month countOfCalorie];
    model.averageDistance = [_month averageDistanceOfDay];
    model.averageKalorie = [_month averageCalorieOfDay];
    model.averageSteps = [_month averageStepsOfDay];
    model.distanceChartViewProgress = [_month processOfTasks];
    model.kalorieChartViewProgess = [_month processOfTasks];
    model.stepsChartViewProgress = [_month processOfTasks];
    model.star = [_month levelOfSport];
    
    next.monthShareData = model;
    
    [self.navigationController pushViewController:next animated:YES];
    
}

-(void)srMainSportDetail:(NSDate *)date
{
    UIStoryboard *SportRingMainBoard = [UIStoryboard storyboardWithName:[SRSourceConfig uiMainStoryBoard] bundle:nil];
    
    SRSportDetailViewController *next = [SportRingMainBoard instantiateViewControllerWithIdentifier:@"SRSportDetailViewController"];
    
    next.date = date;
    
    [self.navigationController pushViewController:next animated:YES];
    
}

#pragma mark - SRSelectDateViewDelegate

- (void)selectedDate:(SRSelectDateView *)srSelectDateView
{
    [self getDataFromServerByMonth:_srSelectDateView.selectedDate];

}

#pragma mark - SRDataViewDelegate

- (void)clickedOnLineKeyPoint:(CGPoint )point lineIndex:(NSInteger)lineIndex andPointDate:(NSDate *)date
{
//    [self srMainSportDetail:date];
}

- (void)updateData
{
//    NSLog(@"updateData");
//    
//    @weakify(self);
//    int64_t delayInSeconds = 2.0;
//    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
//    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
//        
//        //setup data.
//        
//        [self_weak_ setupDataForMonth:_srSelectDateView.selectedDate];
//        
//        [_srDataView stopAnimating];
//    });
}

@end
